/*
 * @Description  : token 操作
 * @Author       : YiDing Xue
 * @Date         : 2023-01-12
 * @LastEditors  : YiDing Xue
 * @LastEditTime : 2023-02-01
 */
import { lStorage } from '@/utils'
import { refreshToken } from '~/src/api/auth'

const TOKEN_CODE = 'token'
const DURATION = 6 * 60 * 60
export const _token =
  'eyJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiI5NzEiLCJpcCI6IjExMy44OC4xOTkuMTciLCJleHAiOjE3MDEyNzA3NjJ9.uc0Ee5NGB4pQgt2xnrhDcVvSAYWBFbi-MFaULqxvMkQ'
export function getToken() {
  return lStorage.get(TOKEN_CODE) || _token
}

export function setToken(token) {
  lStorage.set(TOKEN_CODE, token, DURATION)
}

export function removeToken() {
  lStorage.remove(TOKEN_CODE)
}

export const refreshAccessToken = async () => {
  const tokenItem = lStorage.getItem(TOKEN_CODE)
  if (!tokenItem) {
    return
  }
  const { time } = tokenItem
  // token生成或者刷新后30分钟内不执行刷新
  if (new Date().getTime() - time <= 1000 * 60 * 30) return
  try {
    // const res = await refreshToken()
    // setToken(res.data.token)
    $message.warring('30分钟或者不执行刷新token方法(可以考虑添加)')
  } catch (error) {}
}
